import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
 
public class TomReceive implements Runnable {
	DatagramSocket ds =null;
	DatagramPacket dp=null;
	
	public TomReceive(DatagramSocket ds) {
		this.ds = ds;
	}
 
	@Override
	public void run() {
	
	try {
		while(true){
			byte[] buf=new byte[1024];
			dp=new DatagramPacket(buf, buf.length);
			ds.receive(dp);
			String id =dp.getAddress().getHostAddress();//获得ip 
			int port=dp.getPort();//获得端口号
			String str =new String(dp.getData(), 0, dp.getLength());
			
			if("over".equalsIgnoreCase(str)){
				System.out.println("对方离开了聊天室");
				break;
			}
			System.out.println("ip:--"+id+",端口："+port+"：："+str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ds.close();
		}
		}
}